import { BadGatewayException, Body, Controller, Get, Post, Query, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';
import { LunboModel } from 'src/mongodb/models/lunboModel/lunboModel';
import { searchAppUserDto } from '../appUserApi/appUserDto/appUserDto';
import { deleteAdvDto, getAdvDto } from './lunboDto/lunboDto';

@Controller('web')
@UseGuards(AuthGuard("jwtweb"))
export class AdvController {
    constructor(@InjectModel(LunboModel) private lunbo: ModelType<LunboModel>) { }

    /* 增 */
    @Post('addAdv')
    @UseInterceptors(FileInterceptor('file'))
    async addAdv(@UploadedFile() file, @Body() body) {
        try {
            const time = Math.floor(Date.now() / 1000)
            await this.lunbo.create({
                url: body.url,
                type: body.radio,
                imgurl: `public/advimg/${file.filename}`,
                createTime: time,
                updateTime: time
            })
            return { code: 200, message: '上传成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }


    /* 删 */
    @Post('deleteAdv')
    async deleteAdv(@Body() body: deleteAdvDto) {
        try {
            await this.lunbo.deleteOne({ _id: body.id })
            return { code: 200, message: '删除成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }


    /* 改 */
    @Post('updateAdv')
    async updateAdv(@Body() body) {
        try {
            const time = Math.floor(Date.now() / 1000)
            await this.lunbo.updateOne({ _id: body.id }, { ...body, updateTime: time })
            return { code: 200, message: '修改成功' }
        } catch (error) {
            throw new BadGatewayException(error)
        }
    }


    /* 查 */
    @Get('getAdv')
    async getAdv(@Query() query: getAdvDto) {
        try {
            let data: { pageNo: number | string, pageSize: number | string } = query
            if (typeof data.pageNo == 'string') {
                data.pageNo = parseInt(data.pageNo)
            }
            if (typeof data.pageSize == 'string') {
                data.pageSize = parseInt(data.pageSize)
            }
            const total = await this.lunbo.countDocuments()
            const userlist = await this.lunbo.find().skip((data.pageNo - 1) * data.pageSize).limit(data.pageSize).sort({ '_id': -1 });
            return { total, data: userlist }

        } catch (error) {
            throw new BadGatewayException(error)
        }
    }

    /*  搜索广告*/
    @Get('searchAdv')
    async searchAdv(@Query() query: searchAppUserDto) {
        try {
            const userlist = await this.lunbo.find({ type: query.name })
            return { total: userlist.length, data: userlist }

        } catch (error) {
            throw new BadGatewayException(error)
        }
    }
}
